<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="header::header" />
<body>
	<div id="dgtoolbar">
		<a id="newbtn" href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true">新增</a>
		<a id="deletebtn" href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
		<a id="reloadbtn" href="#" class="easyui-linkbutton" iconCls="icon-reload" plain="true">刷新</a>
	</div>
	<table id="dg" class="easyui-datagrid" data-options="url:'pageQuery',idField:'iid',rownumbers:true,singleSelect:false,method:'post',toolbar:'#dgtoolbar',pagination:true,pageSize:20,pageList:[20,30,50],fit:true,onLoadSuccess:onDGLoadSuccess">
		<thead>
			<tr>
				<th data-options="field:'iid'" checkbox="true" />
				<th data-options="field:'cname'" width="150" align="center">角色名称</th>
				<th data-options="field:'cvisible'" width="150" align="center">可见</th>
				<th data-options="field:'isort'" width="80" align="center">排序</th>
				<th data-options="field:'ccomment'" width="200" align="center">备注</th>
				<th data-options="field:'opt',formatter:onItemOptEdit" width="40" align="center">修改</th>
				<th data-options="field:'opt1',formatter:onItemOptDelete" width="40" align="center">删除</th>
				<th data-options="field:'opt2',formatter:onItemOptShow" width="40" align="center">查看</th>
				<th data-options="field:'opt3',formatter:onMenuChoose" width="60" align="center">关联菜单</th>
			</tr>
		</thead>
	</table>
	<div id="dlg" class="easyui-dialog" title="操作" style="padding: 5px" data-options="buttons: '#dlg-buttons',closed:'true',modal:true">
		<form id="dataform" method="post">
			<table>
				<tr>
					<td><input name="iid" id="iid" type="hidden"></input>
					<input name="cvisible" id="cvisible" type="hidden"></input>
				</tr>
				<tr>
					<td>角色名称:</td>
					<td><input name="cname" class="easyui-textbox" style="width: 150px" data-options="required:true"></input></td>
				</tr>
				<tr>
                        <td>可见权限:</td>
                        <td><input id="ivisible" name="ivisible" class="easyui-combobox" style="width: 150px" data-options="panelHeight:'auto',url:'../dicdetails/findItems?ckey=visible',valueField:'cvalue',textField:'cname',editable:false,required:true"></input></td>
                    </tr>
				<!--  <tr>
                     <td>菜单权限:</td>
                     <td><input id="ipid" class="easyui-combotree" style="width: 150px" data-options="url:'../menu/treeQuery',panelHeight:'200px',required:true,multiple:'false'" />
                         <input type="hidden" name="imenuid" id="imenuid" value=""/>
                     </td>
                 </tr> -->
				<tr>
					<td>排序:</td>
					<td><input name="isort" class="easyui-numberspinner" style="width: 150px" data-options="min:0,max:100,required:true"></input></td>
				</tr>
				<tr>
					<td>备注:</td>
					<td><input name="ccomment" class="easyui-textbox" style="width: 150px" data-options="required:false"></input></td>
				</tr>
			</table>
		</form>
	</div>
	<div id="dlg-buttons">
		<a id="dlg-savebtn" href="javascript:void(0)" style="width: 50" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
		<a id="dlg-closebtn" href="javascript:void(0)" style="width: 50" class="easyui-linkbutton" iconCls="icon-no">关闭</a>
	</div>
	<div id="dlg-menuChoose" class="easyui-dialog" title="关联菜单" style="padding: 5px;height: 300px;width: 200px;" data-options="buttons: '#dlg-menuButton',closed:'true',modal:true">
		<form id="dataform-menu" method="post">
			<div id="treeMenu" class="easyui-tree"
				 data-options="url:'../menu/treeQuery',multiple:'true',checkbox:'true'"></div>
			<input type="hidden" name="iroleid" id="iroleid" value=""/>
			<input type="hidden" name="imenuid" id="imenuid" value=""/>
		</form>
	</div>
	<div id="dlg-menuButton">
		<a id="dlg-menu-savebtn" href="javascript:void(0)" style="width: 50" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
		<a id="dlg-menu-closebtn" href="javascript:void(0)" style="width: 50" class="easyui-linkbutton" iconCls="icon-no">关闭</a>
	</div>
</body>
<script type="text/javascript">
$("#ivisible").combobox({
	onChange : function() {
		$("#cvisible").val($("#ivisible").combobox("getText"));
	}
})
	function onMenuChoose(value, row, index) {
		return "<a class='easyui-linkbutton dgMenuChoose' style='width:25px;height:16px;' iconCls='icon-redo' onclick='onMenu("
				+ index + ")' title='查看' plain='true'></a> ";
	}
	// 打开菜单弹窗
	function onMenu(index){
		$("#treeMenu").tree("reload");
		var rowData = getRowById(index);
		$("#iroleid").val(rowData.iid);
		var idarr = new Array();
		var url="../menu/getMenuIds?rid="+rowData.iid;
		$.ajax({
				type : "post",
				url : url,
		 		dataType : "json" ,
				success : function(data) {
					 idarr = data.data.split(",");
					
					 for (var i = 0; i < idarr.length; i++) {
						var node = $("#treeMenu").tree("find",parseInt(idarr[i]));
						
						var children = $("#treeMenu").tree("getChildren", node.target);
						console.log(children.length)
			            if (children.length == 0) {
			                $("#treeMenu").tree('check', node.target);
			            }
					}
				}
			});
	
		$("#dlg-menuChoose").dialog("open");
	}
	//勾选选中的菜单
	function treeChoose(ids){
		var idarr = new Array();
	}
	// 保存并提交已经勾选的菜单信息
	function onSubmitMenu(){
		//$.messager.progress();	
		$("#dataform-menu").form("submit",{
			url:"insertRoleMenu",
			onSubmit: function(){
				/* var isValid = $(this).form('validate');
				if (!isValid){
					$.messager.progress('close');	
				}
				return isValid; */
				return true;
			},
			success:function(data){
				 var data = eval('(' + data + ')');  // change the JSON string to javascript object
		       	 if (data.success){
					$.messager.alert("提示",data.msg);
					$("#dlg-menuChoose").dialog("close");
		       	 }
			},
			error:function(){
				$.messager.alert("提示","出错了，请联系管理员！");
			}
		});
	}
	$(document).ready(function(){
		// 勾选菜单保存
		$("#dlg-menu-savebtn").click(function(){
			onSubmitMenu();
		});
		// 关闭或取消菜单弹窗
		$("#dlg-menu-closebtn").click(function(){
			$("#dlg-menuChoose").dialog("close");
		})
	/* 	$("#ipid").combotree({
			onChange:function(newValue, oldValue){
				var t=$("#ipid").combotree("tree");
				var arrTree = t.tree("getChecked", ["checked","indeterminate"]);
				ids = Common.getIds(arrTree,"iid");
				$("#imenuid").val(ids);
			}
		}); */
		//获取勾选菜单的iid
		$("#treeMenu").tree({
			onCheck:function(node, checked){
				var arrTree = $("#treeMenu").tree("getChecked", ["checked","indeterminate"]);
				ids = Common.getIds(arrTree,"iid");
				$("#imenuid").val(ids);
			}
		})
	});
</script>
<script type="text/javascript" th:src="@{/common/GridView.js}"></script>
</html>